package com.jmyd.txlcn.base.vo;

import com.jmyd.txlcn.enums.SystemErrorEnum;
import org.springframework.util.StringUtils;

import java.util.Locale;

/**
 * api统一返回对象
 *
 * @author samphin
 * @date 2020-8-31 16:41:15
 */
public class Result<T> {

    /**
     * 成功标识 成功=true 失败=false
     */
    private boolean success = true;
    /**
     * 状态码
     */
    private int code = 200;
    /**
     * 消息
     */
    private String message;
    /**
     * 数据对象
     */
    private T data;

    /**
     * 获取系统默认的Locale
     */
    private static final Locale SYSTEM_LOCALE;

    static {
        SYSTEM_LOCALE = Locale.getDefault();
    }

    public Result() {
    }

    public Result(boolean success, int code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result(T data) {
        this.data = data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public static <T> Result<T> ok() {
        return new Result<>();
    }

    public static <T> Result<T> ok(int code, String message) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.TRUE);
        result.setMessage(message);
        result.setCode(code);
        return result;
    }

    public static <T> Result<T> ok(String message) {
        Result<T> result = new Result<>();
        result.setMessage(message);
        return result;
    }

    /**
     * 针对特定的错误返回时，success=true
     *
     * @param systemErrorEnum 错误码
     * @author samphin
     * @date 2021-5-21 14:45:30
     */
    public static <T> Result<T> ok(SystemErrorEnum systemErrorEnum) {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setCode(systemErrorEnum.getErrorCode());
        result.setMessage(systemErrorEnum.getErrorMsg());
        return result;
    }

    public static <T> Result<T> failure(int code, String message) {
        return failure(Boolean.FALSE, code, message);
    }

    public static <T> Result<T> failure(boolean success, int code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setSuccess(success);
        if (StringUtils.isEmpty(message)) {
            message = getLocalizedMessage();
        }
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> failure() {
        return failure("");
    }

    public static <T> Result<T> failure(String message) {
        return failure(-400, message);
    }

    public static <T> Result<T> failure(SystemErrorEnum systemErrorEnum) {
        return failure(systemErrorEnum.getErrorCode(), systemErrorEnum.getErrorMsg());
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return (null == this.message || this.message.length() == 0) ? getLocalizedMessage() : this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public static String getLocalizedMessage() {
        String language = SYSTEM_LOCALE.getLanguage();
        if (Locale.CHINESE.getLanguage().equals(language)) {
            return "操作成功";
        } else {
            return "success";
        }
    }
}
